using System;
using System.Collections;
using System.Runtime.CompilerServices;
using UnityEngine;
using Voodoo.Sauce.Internal.Ads.MopubMediation;
using Voodoo.Sauce.Internal.Analytics;
using Voodoo.Sauce.Internal.GDPR;
using Voodoo.Sauce.Internal.IAP;

namespace Voodoo.Sauce.Internal.Ads
{
	internal static class VoodooAds
	{
		private const string TAG = "VoodooAds";

		private static BannerAd _bannerAd;

		private static InterstitialAd _interstitialAd;

		private static RewardedVideoAd _rewardedVideoAd;

		private static InterstitialAdDisplayConditions _interstitialAdDisplayConditions;

		private static bool _isSdkInitialized;

		private static bool _bannerShown;

		private static bool _bannerShouldShow;

		private static Action<float> _bannerCallback;

		private static string _bannerAdUnit;

		private static string _interstitialAdUnit;

		private static string _rewardedVideoAdUnit;

		private static MediationType _currentMediationType;

		private const string kMediationType = "VoodooAds_MediationType";

		[CompilerGenerated]
		private static Action<string> _003C_003Ef__mg_0024cache0;

		[CompilerGenerated]
		private static Action<string> _003C_003Ef__mg_0024cache1;

		public static void InitializeMopub()
		{
			string text = _bannerAdUnit;
			if (string.IsNullOrEmpty(text))
			{
				text = _interstitialAdUnit;
			}
			if (string.IsNullOrEmpty(text))
			{
				text = _rewardedVideoAdUnit;
			}
			VoodooLog.Log("VoodooAds", "Initializing Mopub with adUnit: " + text);
			MoPubManager.OnSdkInitalizedEvent += OnSdkInitialized;
			MoPubBase.SdkConfiguration sdkConfiguration = default(MoPubBase.SdkConfiguration);
			sdkConfiguration.AdvancedBidders = new MoPubBase.AdvancedBidder[2]
			{
				MoPubBase.AdvancedBidder.Facebook,
				MoPubBase.AdvancedBidder.AdColony
			};
			sdkConfiguration.AdUnitId = text;
			sdkConfiguration.NetworksToInit = new MoPubBase.RewardedNetwork[10]
			{
				MoPubBase.RewardedNetwork.Chartboost,
				MoPubBase.RewardedNetwork.Facebook,
				MoPubBase.RewardedNetwork.Tapjoy,
				MoPubBase.RewardedNetwork.Unity,
				MoPubBase.RewardedNetwork.Vungle,
				MoPubBase.RewardedNetwork.AdColony,
				MoPubBase.RewardedNetwork.AdMob,
				MoPubBase.RewardedNetwork.AppLovin,
				MoPubBase.RewardedNetwork.IronSource,
				MoPubBase.RewardedNetwork.OnebyAOL
			};
			MoPubAndroid.InitializeSdk(sdkConfiguration);
		}

		private static void OnSdkInitialized(string adUnit)
		{
			MoPubManager.OnSdkInitalizedEvent -= OnSdkInitialized;
			VoodooLog.Log("VoodooAds", "Mopub Initialized");
			VoodooLog.Log("VoodooAds", $"Granting consent to MoPub: applicable: {VoodooGDPR.IsGdprApplicable()} - consent: {VoodooGDPR.HasAdsConsent()}");
			if (VoodooGDPR.IsGdprApplicable())
			{
				SetConsent(VoodooGDPR.HasAdsConsent());
			}
			_isSdkInitialized = true;
			VoodooSauceBehaviour.InvokeCoroutine(RequestAds());
		}

		private static IEnumerator RequestAds()
		{
			yield return new WaitForSeconds(0.1f);
			SetAdUnit(AdUnitType.Banner, _bannerAdUnit);
			yield return null;
			SetAdUnit(AdUnitType.Interstitial, _interstitialAdUnit);
			yield return null;
			SetAdUnit(AdUnitType.RewardedVideo, _rewardedVideoAdUnit);
			yield return null;
			if (_bannerShouldShow)
			{
				VoodooLog.Log("VoodooAds", "Showing banner called before initialization ...");
				ShowBanner(_bannerCallback);
				_bannerShouldShow = false;
				_bannerCallback = null;
			}
		}

		public static void Init(AdUnits adUnits)
		{
			VoodooLog.Log("VoodooAds", $"Initializing Ads with: {adUnits.BannerAdUnit}, {adUnits.InterstitialAdUnit}, {adUnits.RewardedVideoAdUnit}");
			_bannerAdUnit = adUnits.BannerAdUnit;
			_interstitialAdUnit = adUnits.InterstitialAdUnit;
			_rewardedVideoAdUnit = adUnits.RewardedVideoAdUnit;
			if (_interstitialAdDisplayConditions == null)
			{
				_interstitialAdDisplayConditions = new InterstitialAdDisplayConditions();
			}
			_currentMediationType = MediationType.Mopub;
		}

		public static MediationType GetCurrentMediation()
		{
			if (_currentMediationType == MediationType.None)
			{
				if (!PlayerPrefs.HasKey("VoodooAds_MediationType"))
				{
					VoodooLog.Log("VoodooAds", "VoodooAds: First time app launch, selecting mediation...");
					int value = UnityEngine.Random.Range(0, 2);
					PlayerPrefs.SetInt("VoodooAds_MediationType", value);
				}
				MediationType @int = (MediationType)PlayerPrefs.GetInt("VoodooAds_MediationType", 0);
				VoodooLog.Log("VoodooAds", "VoodooAds: Current mediation: " + @int);
				_currentMediationType = @int;
			}
			return _currentMediationType;
		}

		internal static void SetAdUnit(AdUnitType adUnitType, string adUnit)
		{
			if (_isSdkInitialized)
			{
				VoodooLog.Log("VoodooAds", $"Mopub is initialized - Setting {adUnitType.ToString()} adunit {adUnit}");
				if (adUnitType == AdUnitType.Banner && !VoodooPremium.IsPremium())
				{
					UpdateBannerAdUnit(adUnit);
				}
				else if (adUnitType == AdUnitType.Interstitial && !VoodooPremium.IsPremium())
				{
					UpdateInterstitialAdUnit(adUnit);
				}
				else if (adUnitType == AdUnitType.RewardedVideo)
				{
					UpdateRewardedVideoAdUnit(adUnit);
				}
			}
			else
			{
				if (adUnitType == AdUnitType.Banner && !VoodooPremium.IsPremium())
				{
					_bannerAdUnit = adUnit;
				}
				else if (adUnitType == AdUnitType.Interstitial && !VoodooPremium.IsPremium())
				{
					_interstitialAdUnit = adUnit;
				}
				else if (adUnitType == AdUnitType.RewardedVideo)
				{
					_rewardedVideoAdUnit = adUnit;
				}
				VoodooLog.Log("VoodooAds", $"Mopub is not initialized. Ignoring ad unit {adUnitType.ToString()} {adUnit}");
			}
		}

		public static void SetInterstitialAdsDisplayConditions(int delayInSecondsBeforeFirstInterstitialAd, int delayInSecondsBetweenInterstitialAds, int maxGamesBetweenInterstitialAds)
		{
			_interstitialAdDisplayConditions = new InterstitialAdDisplayConditions(delayInSecondsBeforeFirstInterstitialAd, delayInSecondsBetweenInterstitialAds, maxGamesBetweenInterstitialAds);
		}

		public static void ShowBanner(Action<float> onBannerDisplayed)
		{
			if (VoodooPremium.IsPremium())
			{
				VoodooLog.Log("VoodooAds", "User is premium, not displaying banner");
				return;
			}
			if (!_isSdkInitialized)
			{
				VoodooLog.Log("VoodooAds", "Banner will show after MoPub init");
				_bannerShouldShow = true;
				_bannerCallback = onBannerDisplayed;
				return;
			}
			if (_bannerAd == null)
			{
				VoodooLog.Log("VoodooAds", "No registered banner ad unit.");
				return;
			}
			if (_bannerShown)
			{
				VoodooLog.Log("VoodooAds", "Banner is already showing");
				return;
			}
			_bannerShown = true;
			if (onBannerDisplayed != null)
			{
				BannerAd bannerAd = _bannerAd;
				bannerAd.OnAdShown = (Action<float>)Delegate.Combine(bannerAd.OnAdShown, onBannerDisplayed);
			}
			VoodooLog.Log("VoodooAds", "Showing banner ...");
			_bannerAd.Show();
		}

		public static void HideBanner()
		{
			if (!_isSdkInitialized)
			{
				VoodooLog.Log("VoodooAds", "Banner will no longer show after MoPub init");
				_bannerShouldShow = false;
				_bannerCallback = null;
			}
			else if (_bannerAd == null)
			{
				VoodooLog.Log("VoodooAds", "No registered banner ad unit.");
			}
			else if (!_bannerShown)
			{
				VoodooLog.Log("VoodooAds", "Banner is not displayed");
			}
			else
			{
				_bannerShown = false;
				VoodooLog.Log("VoodooAds", "Hiding banner ...");
				_bannerAd.Hide();
			}
		}

		public static bool IsBannerShowing()
		{
			return _bannerShown;
		}

		internal static bool IsInterstitialAvailable()
		{
			return _interstitialAd != null && _interstitialAd.IsLoaded();
		}

		public static void ShowInterstitial(Action onComplete)
		{
			if (VoodooPremium.IsPremium())
			{
				VoodooLog.Log("VoodooAds", "User is premium, not displaying interstitial");
			}
			else if (_interstitialAd == null)
			{
				VoodooLog.Log("VoodooAds", "No registered interstitial ad unit.");
			}
			else if (!_interstitialAdDisplayConditions.AreConditionsMet())
			{
				VoodooLog.Log("VoodooAds", "Interstitial ad display conditions not met.");
			}
			else
			{
				if (_interstitialAd.IsLoaded())
				{
					VoodooLog.Log("VoodooAds", "Displaying an interstitial ad ...");
					InterstitialAd interstitialAd = _interstitialAd;
					interstitialAd.OnComplete = (Action)Delegate.Combine(interstitialAd.OnComplete, new Action(_interstitialAdDisplayConditions.InterstitialDisplayed));
					if (onComplete != null)
					{
						InterstitialAd interstitialAd2 = _interstitialAd;
						interstitialAd2.OnComplete = (Action)Delegate.Combine(interstitialAd2.OnComplete, onComplete);
					}
					VoodooAnalytics.OnFsShown();
					_interstitialAd.Show();
					return;
				}
				VoodooLog.Log("VoodooAds", "Interstitial ad is not loaded yet.");
			}
			onComplete?.Invoke();
		}

		public static bool IsRewardedVideoAvailable()
		{
			return _rewardedVideoAd != null && _rewardedVideoAd.IsLoaded();
		}

		public static void ShowRewardedVideo(Action<bool> onComplete)
		{
			if (_rewardedVideoAd == null)
			{
				VoodooLog.Log("VoodooAds", "No registered rewarded video ad unit.");
			}
			else
			{
				if (_rewardedVideoAd.IsLoaded())
				{
					VoodooLog.Log("VoodooAds", "Displaying a rewarded video ad ...");
					RewardedVideoAd rewardedVideoAd = _rewardedVideoAd;
					rewardedVideoAd.OnComplete = (Action<bool>)Delegate.Combine(rewardedVideoAd.OnComplete, new Action<bool>(_interstitialAdDisplayConditions.RewardedVideoDisplayed));
					if (onComplete != null)
					{
						RewardedVideoAd rewardedVideoAd2 = _rewardedVideoAd;
						rewardedVideoAd2.OnComplete = (Action<bool>)Delegate.Combine(rewardedVideoAd2.OnComplete, onComplete);
					}
					VoodooAnalytics.OnRvShown();
					_rewardedVideoAd.Show();
					return;
				}
				VoodooLog.Log("VoodooAds", "Rewarded video ad is not loaded yet.");
			}
			onComplete?.Invoke(obj: false);
		}

		public static void OnGamePlayed()
		{
			if (_interstitialAdDisplayConditions != null)
			{
				_interstitialAdDisplayConditions.GamePlayed();
			}
		}

		private static void UpdateBannerAdUnit(string adUnit)
		{
			if (_bannerAd != null)
			{
				_bannerAd.Destroy();
				_bannerAd = null;
			}
			if (string.IsNullOrEmpty(adUnit))
			{
				VoodooLog.Log("VoodooAds", "Banner ad unit is empty, no banners will be fetched or displayed.");
				return;
			}
			MediationType currentMediationType = _currentMediationType;
			if (currentMediationType == MediationType.Mopub)
			{
				_bannerAd = new MopubBanner(adUnit);
			}
		}

		private static void UpdateInterstitialAdUnit(string adUnit)
		{
			if (_interstitialAd != null)
			{
				_interstitialAd.Destroy();
				_interstitialAd = null;
			}
			if (string.IsNullOrEmpty(adUnit))
			{
				VoodooLog.Log("VoodooAds", "Interstitial ad unit is empty, no interstitial ad will be fetched or displayed.");
				return;
			}
			MediationType currentMediationType = _currentMediationType;
			if (currentMediationType == MediationType.Mopub)
			{
				_interstitialAd = new MopubInterstitial(adUnit, autofetch: true);
			}
		}

		private static void UpdateRewardedVideoAdUnit(string adUnit)
		{
			if (_rewardedVideoAd != null)
			{
				_rewardedVideoAd.Destroy();
				_rewardedVideoAd = null;
			}
			if (string.IsNullOrEmpty(adUnit))
			{
				VoodooLog.Log("VoodooAds", "Rewarded video ad unit is empty, no rewarded video ad will be fetched or displayed.");
				return;
			}
			MediationType currentMediationType = _currentMediationType;
			if (currentMediationType == MediationType.Mopub)
			{
				_rewardedVideoAd = new MopubRewardedVideo(adUnit, autofetch: true);
			}
		}

		public static void DisableBannerAndInterstitial()
		{
			if (_bannerAd != null)
			{
				_bannerAd.Destroy();
				_bannerAd = null;
			}
			if (_interstitialAd != null)
			{
				_interstitialAd.Destroy();
				_interstitialAd = null;
			}
		}

		public static void SetConsent(bool consent)
		{
			if (consent)
			{
				MoPubAndroid.PartnerApi.GrantConsent();
			}
			else
			{
				MoPubAndroid.PartnerApi.RevokeConsent();
			}
		}
	}
}
